/*
 *公用js，系统封装的数据绑定方法放在这里
 */
//配置文件，postRouter
var router = "http://localhost:52254/api";

var Data = {};
//页面绑定数据源
Data["Fill"] = function (obj, converts) {
    var data = this.ParseData(obj);
    if (!data) {
        return;
    }
    var uiCollection = $('[binding]');
    uiCollection.each(function (val) {
        var cur = $(this);
        cur.html('');
        var keys = cur.attr("binding").split(',');
        var convert = cur.attr("converter");
        var _html = '';
        for (var i = 0; i < keys.length; i++) {
            var key = keys[i];
            if (convert) {
                try {
                    var converter = converts[convert];
                    var ret = typeof (converter) == 'function' && converter(data[key],cur);
                    _html += ret;
                } catch (err) {
                    console.error(err);
                }
            } else {
                if (!data[key]) {
                    _html = '';
                    continue;
                }else{
                    _html += data[key];
                }
            }
        }
        cur.html(_html);
    });
};
Data["ParseData"] = function (obj) {
    var result = {};
    var data = null;
    if (obj.Status != 0) {
        //服务器抛错
        alert('服务器内部错误请联系管理员');
        return false;
    }
    if (obj.Data && obj.Data.List) {
        data = obj.Data.List;
    } else {
        data = obj.Data;
    }
    if (!data || data.length < 0) {
        alert('未找到当前数据');
        return null;
    }
    if (data.length > 0) {
        result = data[0];
    } else {
        result = data;
    }
    return result;
};
Data["CollectPara"] = function (conter) {
    var formel = $(conter).find('[name]');
    var result = {};
    formel.each(function (key, val) {
        var cur = $(this);
        var names = cur.attr("name");
        result[names] = cur.val();
    });
    return result;
};

//扩展window.location方法
QueryParse();

function QueryParse() {
    var result = {};
    var parastr = window.location.href.split('?').length > 1 ? window.location.href.split('?')[1] : '';
    var paragroup = parastr.split("&");
    for (var i = 0; i < paragroup.length; i++) {
        var item = paragroup[i].split('=');
        var key = item[0];
        var val = item[1];
        result[key] = val;
    }
    window.location['query'] = result;
};
//是否存在cookie中用户名和密码，不存在则返回登录页
function existcookie() {
    var username = Cookie.getCookie("username");
    var password = Cookie.getCookie("password");
    if (username == "" || password == "") {
        window.location.href = "login.html";
    }
}
var Cookie = {
    
};
//设置cookie
Cookie.setCookie = function (c_name, value, expiredays) {
    var exdate = new Date()
    exdate.setDate(exdate.getDate() + expiredays)
    document.cookie = c_name + "=" + escape(value) +
        ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString())
}

//获取cookie
Cookie.getCookie = function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=")
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1
            c_end = document.cookie.indexOf(";", c_start)
            if (c_end == -1) c_end = document.cookie.length
            return unescape(document.cookie.substring(c_start, c_end))
        }
    }
    return ""
}
///检查cookie
Cookie.checkCookie = function checkCookie() {
    username = getCookie('username')
    if (username != null && username != "")
        return 0;
    else {
        if (username != null && username != "") {
            setCookie('username', username, 365)
        }
    }
    return 1;
}
$(function(){ 
$('img').off("load").on('load',function(){
    var src = $(this).attr('src');
    if(src==null||src.toLowerCase()=='unkown'||src==''){
        $(this).attr('src', './img/loading_bg_default.png');
    }
    });
    $('img').off('error').on('error',function(){
        $(this).attr('src', './img/loading_bg_defealt.png');
    });
});